import Singleton from "../net/Singleton";
import ResManager from "./ResManager";
import UIBase from "./UIBase";

export default class UIManager extends Singleton {
    _uimap: {} = {};

    curscene = cc.director.getScene()
    private _GetUIRoot(mode) {
        return cc.find('Canvas/UIRoot');
    }

    // 以场景为分组管理UI
    // 常驻节点的UI分组为‘persist’
    // 如果当前节点树中已经存在Ui，则直接设置显示状态为true。否则需要加载
    OpenUI(name, { mode = 0, group = this.curscene.name, params = null } = {}) {
        let needload = true;
        let root = this._GetUIRoot(mode);
        cc.log(`打开界面${name}`);
        if (this._CheckHasUI(name)) {
            let { info, node } = this._GetCurUI(name);
            if (info.mode == 1 && node) {
                node.active = true;
                needload = false;
                let jsCtrl = node.getComponent(name)
                if (jsCtrl && jsCtrl.init) {
                    jsCtrl.init()
                }
            }
        }
        if (needload) {
            let UILoading = cc.find('Canvas/UILoading');
            UILoading && (UILoading.active = true);
            ResManager.ins().loadSingle(name, 'prefab', (prefab) => {
                let node = cc.instantiate(prefab);
                node.parent = root;
                let uibase = node.getComponent(UIBase);
                if (uibase) {
                    uibase.name = name;
                    uibase.params = params;
                }
                let uiinfo = { node, info: { name, mode, group } };
                this._AddUI(uiinfo);
                UILoading && (UILoading.active = false);
            }, root)
        }
    }

    _getUIByName(name, group = this.curscene.name): any[] {
        return this._GetUI({ name, group });
    }

    _GetUI({ name, group }) {
        let uigroup = this._GetGroup(group);
        if (!uigroup[name]) {
            uigroup[name] = [];
        }
        return uigroup[name];
    }

    _GetGroup(group = this.curscene.name) {
        if (!this._uimap[group]) {
            this._uimap[group] = {};
        }
        return this._uimap[group];
    }

    private _OpenUI(name, mode, root) {

    }

    CloseUI(name) {
        let uiinfo = this._GetCurUI(name);
        if (uiinfo) {
            let { node, info } = uiinfo;
            if (info.mode == 0) {
                node.destroy();
                this._getUIByName(name).pop();
            }
            if (info.mode == 1) {
                (node as cc.Node).active = false;
            }
        }
        cc.log(`关闭界面${name}`);
    }

    _AddUI(uiinfo: { info: { name, mode, group }, node }) {
        this._getUIByName(uiinfo.info.name).push(uiinfo);
    }

    _RemoveUI(name) {
        let nodearr = this._getUIByName(name) as any;
        if (nodearr.length > 0) {
            let { node, info } = nodearr[nodearr.length - 1];
            if (node) {
                node.destroy();
                this._getUIByName(name).pop();
            }
        }
    }

    _CheckHasUI(name) {
        let uiarr = this._getUIByName(name);
        return uiarr.length > 0;
    }

    _GetCurUI(name) {
        let uiinfo = null;
        let nodearr = this._getUIByName(name) as any;
        if (nodearr.length > 0) {
            uiinfo = nodearr[nodearr.length - 1];
        }
        return uiinfo;
    }

    // 关闭该场景的所有UI
    CloseAllUI() {
    }

    // 清理所有UI
    ClearAllUI(group = this.curscene.name) {
        this._uimap[group] = {};
    }
}